﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.ComponentModel;
using ScrumTable.Common;

#endregion

namespace ScrumTable.BL.DM.DataManagement
{
    /// <summary>
    /// Represents the an event arguments class which contains additional
    /// information about an event if something has changed inside a
    /// domain object.
    /// </summary>
    public class DomainChangedEventArgs : PropertyChangedEventArgs
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        
        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the changed element instance. If this contains a null reference,
        /// the event is assigned to all elements in the collection.
        /// </summary>
        public DomainBase ChangedElement { get; private set; }

        /// <summary>
        /// Returns true if the changed element contains a valid and useable
        /// DomainBase instance which isn't disposed.
        /// </summary>
        public bool IsValidChangedElement
        {
            get
            {
               return ChangedElement != null && !((IDisposableObject)ChangedElement).IsDisposed;
            }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DomainChangedEventArgs instance. This constructor
        /// is protected internal because its needed by derived or package classes only.
        /// </summary>
        internal protected DomainChangedEventArgs(DomainBase element, string property)
            : base(property)
        {
            ChangedElement = element;
        }

        /// <summary>
        /// Creates a new DomainChangedEventArgs instance. This constructor
        /// is protected internal because its needed by derived or package classes only.
        /// </summary>
        protected DomainChangedEventArgs(string property)
            : base(property)
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns true if a the currrent element contains an event which changed
        /// the given property.  If the event contains a "IsDisposed" Property it returns always false
        /// </summary>
        /// <param name="propertyName">Specifies the name of the property to check.</param>
        public virtual bool ContainsPropertyName(string propertyName)
        {
            return IsValidChangedElement && (propertyName == PropertyName);
        }

        /// <summary>
        /// Returns true if a the currrent element contains an event which changed
        /// the given id. If the event contains a "IsDisposed" Property it returns always false
        /// </summary>
        /// <param name="propertyId">Specifies the id of the property to check.</param>
        /// <param name="checkParentId">Specifies if the parent id should contains the id.</param>
        public virtual bool ContainsElementId(string propertyId, bool checkParentId=false)
        {
            return (IsValidChangedElement && (checkParentId ? ChangedElement.Parent.Id == propertyId : ChangedElement.Id == propertyId));
        }

        /// <summary>
        /// Returns true if a the currrent element contains an event with the given type. If the event contains a "IsDisposed" Property it returns always false
        /// </summary>
        /// <param name="checkParentType">Specifies if the parent id should contains the id.</param>
        public virtual bool ContainsType<T>(bool checkParentType=false) where T : class 
        {
            return (IsValidChangedElement && (checkParentType ? ChangedElement.Parent.GetType() == typeof(T) : ChangedElement.GetType() == typeof(T)));
        }

        /// <summary>
        /// Returns true if a the currrent element contains an event which changed
        /// the given property. If the event contains a "IsDisposed" Property it returns always false
        /// </summary>
        /// <param name="propertyName">Specifies the name of the property to check.</param>
        public bool ContainsPropertyNames(params string[] propertyName)
        {

            foreach (string propName in propertyName)
            {
                if (ContainsPropertyName(propName))
                {
                    return true;
                }
            }
            return false;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}